<template>
  <el-form ref="classForm" :model="form" label-width="20px" style="margin-top: 20px">
    <el-form-item class="form-item">
      <el-select v-model="seriesID" placeholder="选择系列" value="" @change="filterChapter">
        <el-option
          v-for="item in series"
          :key="item.id"
          :label="item.name"
          :value="item.id"
        />
      </el-select>
      <el-select v-model="chapterID" placeholder="选择章节" value="" @change="courseList">
        <el-option
          v-for="item in chapterFilter"
          :key="item.id"
          :label="item.name"
          :value="item.id"
        />
      </el-select>
      <el-button
        type="primary"
        style="margin-left: 5px"
        :disabled="!chapterID"
        @click="add"
      >添加关卡
      </el-button>
    </el-form-item>
    <el-row>
      <div v-for="item in courses" :key="item.id" class="card">
        <course-card
          :item="item"
          :list="courses"
          :total="total"
          :chapter="chapterID"
          @edit="edit"
          @reload="courseList(chapterID)"
        />
      </div>
    </el-row>
    <el-dialog :visible.sync="dialog" :title="title">
      <course-dialog
        :form="form"
        :total="total"
        :files="files"
        :chapter="chapterID"
        :mode="isEditMode"
        @close="close"
        @upload="Uploaded"
        @reload="courseList(chapterID)"
      />
    </el-dialog>
  </el-form>

</template>

<script>
import { fetchSimpleChapterList } from '../../api/chapter'
import { fetchCourseList } from '../../api/course'
import { fetchSeriesList } from '../../api/serires'
import CourseCard from './module/CourseCard'
import CourseDialog from './module/CourseDialog'
import { fetchFileList } from '../../api/file'
export default {
  name: 'Course',
  components: { CourseDialog, CourseCard },
  data() {
    return {
      isUpload: false,
      total: null,
      courses: [],
      chapters: [],
      series: [],
      files: [],
      chapterFilter: [],
      chapterID: null,
      seriesID: null,
      title: '',
      dialog: false,
      isEditMode: false,
      query: {
        page: 1,
        limit: null,
        name: null,
        type: 'all',
        sort: '+id'
      },
      form: {
        id: null,
        name: '',
        description: '',
        imageType: '项目快照',
        work_id: null,
        pageURL: '',
        type: '1'
      }
    }
  },
  watch: {
    'form.work_id': function(val) {
      for (const json of this.files) {
        if (json['id'] === val) {
          this.form.pageURL = json['pageURL']
        }
      }
    }
  },
  created() {
    this.seriesList()
    this.getFileList()
  },
  methods: {
    getFileList() {
      fetchFileList(this.query).then(response => {
        if (response.data.total !== 0) {
          this.files = response.data.items
        } else {
          this.files = []
          this.$SUCCESS('请先在文件管理处上传项目文件')
        }
      }).catch(() => {
        this.$ERROR('获取文件列表失败')
      })
    },
    close() {
      this.resetForm()
      this.dialog = false
    },
    add() {
      this.resetForm()
      this.$setDialog(true, '创建关卡', false)
    },
    edit(item) {
      this.$setDialog(true, '更新关卡', true)
      this.form = item
    },
    Uploaded() {
      this.isUpload = true
    },
    resetForm() {
      this.isUpload = false
      this.form = this.$options.data().form
    },
    seriesList() {
      fetchSeriesList().then(response => {
        if (response.data.total !== 0) {
          this.series = response.data.items
          this.seriesID = response.data.items[0].id
          this.chapterList()
        } else {
          this.$WARNING('未创建课程系列')
        }
      }).catch(() => {
        this.$ERROR('获取系列失败')
      })
    },
    chapterList() {
      fetchSimpleChapterList().then(response => {
        if (response.data.total !== 0) {
          this.chapters = response.data.items
          this.filterChapter()
        }
      }).catch(() => {
        this.$ERROR('获取章节失败')
      })
    },
    courseList(id) {
      const chapterID = { chapter: id }
      fetchCourseList(chapterID).then(response => {
        if (response.data.total !== 0) {
          this.total = response.data.total
          this.courses = response.data.items
          this.courses.forEach(item => { item.type = item.type.toString() })
          this.$sort(this.courses)
        } else {
          this.courses = []
          this.$WARNING('该章节没有关卡')
        }
        this.$sort(this.courses)
      }).catch(() => {
        this.$ERROR('获取关卡失败')
      })
    },
    filterChapter() {
      this.chapterID = null
      this.chapterFilter = this.chapters.filter(item => {
        return item.series === this.seriesID
      })
      if (this.chapterFilter.length !== 0) {
        this.chapterID = this.chapterFilter[0].id
        this.courseList(this.chapterID)
      }
    }
  }
}
</script>

<style scoped>
    .card{
        width:330px;
        display:inline-block;
        margin-left: 20px;
    }
</style>
